package beanstao.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

public class PropertyHelper
{
	private Properties config = null;

	public PropertyHelper(String path)
	{
		FileInputStream in = null;
		try
		{
			in = new FileInputStream(new File(path));
		}
		catch (FileNotFoundException e1)
		{
			e1.printStackTrace();
		}
		config = new Properties();
		try
		{
			config.load(in);
			in.close();
		}
		catch (IOException e)
		{
		}
	}

	// 根据key读取value
	public String readValue(String key)
	{
		try
		{
			String value = config.getProperty(key);
			return value;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.err.println("ConfigInfoError" + e.toString());
			return null;
		}
	}

	// 读取properties的全部信息
	public HashMap<String, String> readAllProperties()
	{
		HashMap<String, String> map = new HashMap<String, String>();
		try
		{
			Enumeration<?> en = config.propertyNames();
			while (en.hasMoreElements())
			{
				String key = (String) en.nextElement();
				String Property = config.getProperty(key);
				map.put(key, Property);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.err.println("ConfigInfoError" + e.toString());
		}
		return map;
	}
}
